package com.twu.biblioteca.model;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Library {

    private List<LibraryEntry> libraryEntries;
    protected final static String MOVIE="MOVIE";
    protected final static String BOOK="BOOK";

    public static String getMovieType() {
        return MOVIE;
    }

    public static String getBookType() {
        return BOOK;
    }

    public Library() {
        libraryEntries = new ArrayList<LibraryEntry>();
        libraryEntries.add(new LibraryEntry(new Book(1, "Book One", "A Description about Book One"),BOOK, false));
        libraryEntries.add(new LibraryEntry(new Book(2, "Book Two", "A Description about Book Two"),BOOK, false));
        libraryEntries.add(new LibraryEntry(new Book(3, "Book Three", "A Description about Book Three"),BOOK, false));
        libraryEntries.add(new LibraryEntry(new Movie("Movie One",2000,"Director one",8),MOVIE,false));
        libraryEntries.add(new LibraryEntry(new Movie("Movie Two",2003,"Director two",5),MOVIE,false));
        libraryEntries.add(new LibraryEntry(new Movie("Movie Three",1972,"Director one",10),MOVIE,false));
    }

    public List<Item> getAvailableList(String typeOfItem){
        Iterator<LibraryEntry> libraryEntryIterator = libraryEntries.iterator();
        List<Item> libraryEntryList = new ArrayList<Item>();
        while(libraryEntryIterator.hasNext()){
            LibraryEntry next = libraryEntryIterator.next();
            if(!next.isCheckedOut() && next.getTypeOfItem().equalsIgnoreCase(typeOfItem))
                libraryEntryList.add( next.getItem());
        }
        return libraryEntryList;
    }

    public boolean checkOut(String itemName, String typeOfItem, String username) {
        Iterator<LibraryEntry> libraryEntriesListIterator = libraryEntries.iterator();
        while(libraryEntriesListIterator.hasNext()){
            LibraryEntry libraryEntry = libraryEntriesListIterator.next();
            if(  libraryEntry.getItem().getItemName().equalsIgnoreCase(itemName) && libraryEntry.getTypeOfItem().equalsIgnoreCase(typeOfItem) && !libraryEntry.isCheckedOut() && libraryEntry.getCheckedOutByUser() == null) {
               libraryEntry.toggleCheckedOut();
                libraryEntry.setCheckedOutByUser(username);
                return true;
            }
        }
        return false;
    }

    public boolean returnItem(String itemName, String typeOfItem) {
        Iterator<LibraryEntry> libraryEntriesListIterator = libraryEntries.iterator();
        while(libraryEntriesListIterator.hasNext()){
            LibraryEntry libraryEntry = libraryEntriesListIterator.next();
            if(  libraryEntry.getItem().getItemName().equalsIgnoreCase(itemName) && libraryEntry.getTypeOfItem().equalsIgnoreCase(typeOfItem) && libraryEntry.isCheckedOut() ) {
                libraryEntry.toggleCheckedOut();
                libraryEntry.setCheckedOutByUser();
                return true;
            }
        }
        return false;
    }
}
